<?php

namespace App\Admin\Controllers;


use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Models\User\Users as UsersModel;
use App\Models\User\UserFunds;
use App\Models\User\UserDetail;
use App\Models\Log\LogUserOperation;
use App\Models\Log\LogUserFund;
use App\Models\Option;

class UserController extends BaseController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->model()->orderby('id', 'desc');
            $grid->column('id')->sortable();
            config('admin.users.avatar_show') ? $grid->column('avatar')->image('', 40, 40) : '';
            config('admin.users.nickname_show') ? $grid->column('nickname') : '';
            foreach(config('admin.users.user_identity') as $field){
                $grid->column($field);
            }
            $sys_user = config('admin.users');
            $grid->colum('资金')->display(function() use($sys_user){
                $str = '';
                foreach ($sys_user['user_funds'] as $key => $value) {
                    $str .= $value . ': ' . $this->funds->$key . '<br/>';
                }
                return $str;
            });
            if($sys_user['parent_show']){
                $grid->column('parent_id', '上级ID');
//                $grid->column('parent.phone', '上级标识')->display(function() use($sys_user){
//                    if($this->parent_id == 0){
//                        return "";
//                    }
//                    $identity = $sys_user['user_identity'][0];
//                    return $this->parent->$identity;
//                });
            }
            $grid->column('login_ip','登录IP');
            $grid->column('login_time','登录时间');
            $grid->column('is_login')->switch()->help('如果关闭则此会员无法登录');
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) use($sys_user){
                $filter->equal('id');
                $filter->like('nickname');
                $identity = $sys_user['user_identity'][0];
                $filter->like($identity, '会员标识');
                if($sys_user['parent_show']){
                    $filter->like('parent_id', '上级会员ID');
                    $filter->like('parent.' . $identity, '上级会员标识');
                }
                $filter->equal('is_login')->select(['0'=> '冻结', '1'=> '正常']);
            });
            $grid->export();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(['log_operation', 'detail']), function (Show $show) use($id){
            $show->row(function (Show\Row $show) {
                $show->width(4)->id;
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->avatar->image('', 40, 40);
                $show->width(4)->nickname;
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('detail.general_photo', '全身照')->image('', 40, 40);
                $show->width(4)->field('detail.life_photo', '生活照')->image('', 40, 40);
                $show->width(4)->field('detail.work_photo', '工作照')->image('', 40, 40);
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('detail.study_photo', '学习照')->image('', 40, 40);
                $show->width(4)->field('detail.travel_photo', '旅行照')->image('', 40, 40);
                $show->width(4)->field('detail.other_photo', '其他照')->image('', 40, 40);
            });
            $show->row(function (Show\Row $show) {
                $show->width(3)->field('detail.sex', '性别');
                $show->width(3)->field('detail.birthday', '生日');
                $show->width(3)->field('detail.constellation', '星座');
                $show->width(3)->field('detail.height', '身高');
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('detail.weixin', '微信');
                $show->width(4)->field('detail.qq', 'QQ');
                $show->width(4)->field('detail.email', '邮箱');
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('detail.city', '城市');
                $show->width(4)->field('detail.hometown', '家乡');
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('detail.education', '学历');
                $show->width(4)->field('detail.college', '学院');
                $show->width(4)->field('detail.college_tab', '学院标签');
                $show->width(4)->field('detail.industry', '行业');
                $show->width(4)->field('detail.income', '年收入');
                $show->width(4)->field('detail.car_and_house', '车房情况');
                $show->width(4)->field('detail.marriage', '婚姻情况');
                $show->width(4)->field('detail.family', '家庭情况');
                $show->width(4)->field('detail.account', '户口');
            });
            $show->row(function (Show\Row $show) {
                $show->width(12)->field('detail.hobbies', '兴趣爱好');
                $show->width(12)->field('detail.character', '性格特点');
                $show->width(12)->field('detail.emotional_experience', '情感经历');
                $show->width(12)->field('detail.emotional_view', '情感观');
                $show->width(12)->field('detail.mate_selection', '择偶要求');
                $show->width(12)->field('detail.other', '其他');
                $show->width(12)->field('detail.keyword', '关键词');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(){
        return Form::make(User::with('funds'), function (Form $form) {
            // $form->hidden('password_salt');
            if($form->isCreating()){
                config('admin.users.avatar_show') ? $form->image('avatar')->autoUpload()->uniqueName()->saveFullUrl()->required() : '';
                foreach(config('admin.users.user_identity') as $field){
                    $form->text($field)->required();
                }
                config('admin.users.nickname_show') ? $form->text('nickname')->required() : '';
                $form->text('password')->required();
                if(config('admin.users.level_password_show')){
                    $form->text('level_password')->required();
                }
                if(config('admin.users.parent_show')){
                    $form->select('parent_id')->options(UsersModel::all()->pluck('nickname', 'id'));
                }
                //将输入的密码加密
                $form->saving(function (Form $form) {
                    [$form->password, $form->password_salt] = UsersModel::set_password($form->password);
                    $form->parent_id = $form->parent_id ?? 0;
                });
                // 同步创建资产表与详情表
                $form->saved(function (Form $form, $result) {
                    UserFunds::create_data($result);
                    UserDetail::create_data($result);
                });
            }else{
                $form->tab('基本信息', function(Form $form){
                    $form->display('id');
                    config('admin.users.avatar_show') ? $form->image('avatar')->autoUpload()->uniqueName()->saveFullUrl() : '';
                    foreach(config('admin.users.user_identity') as $field){
                        $form->text($field);
                    }
                    config('admin.users.nickname_show') ? $form->text('nickname') : '';
                });
                // $form->tab('密码', function(Form $form){
                //     $form->text('password')->customFormat(function(){
                //         return '';
                //     })->help('不填写则不修改');
                //     if(config('admin.users.level_password_show')){
                //         $form->text('level_password')->customFormat(function(){
                //             return '';
                //         })->help('不填写则不修改');
                //     }
                // });
                $form->tab('资产', function(Form $form){
                    $user_funds = config('admin.users.user_funds');
                    foreach ($user_funds as $key => $value) {
                        $form->number('funds.' . $key, $value);
                    }
                });
                // $form->tab('详细信息', function(Form $form){
                //     $form->image('detail.general_photo')->autoUpload()->uniqueName()->saveFullUrl();
                //     $form->image('detail.life_photo')->autoUpload()->uniqueName()->saveFullUrl();
                //     $form->image('detail.work_photo')->autoUpload()->uniqueName()->saveFullUrl();
                //     $form->image('detail.study_photo')->autoUpload()->uniqueName()->saveFullUrl();
                //     $form->image('detail.travel_photo')->autoUpload()->uniqueName()->saveFullUrl();
                //     $form->image('detail.other_photo')->autoUpload()->uniqueName()->saveFullUrl();
                //     $form->select('detail.sex')->options(['男'=> '男', '女'=> '女']);
                //     $form->date('detail.birthday')->format('YYYY-MM');
                //     $form->select("detail.face_score")->options(Option::where('type', 'face_score')->pluck('option', 'option'));
                // });
                //判断是否填写了密码，并加密
                // $form->saving(function (Form $form) {
                //     $form->avatar = $form->avatar ?? '';
                //     if($form->password == null){
                //         $form->deleteInput('password');
                //     }else{
                //         [$form->password, $form->password_salt] = UsersModel::set_password($form->password);
                //     }
                //     if($form->level_password == null){
                //         $form->deleteInput('level_password');
                //     }
                // });
                $form->saved(function(Form $form, $result){
                    (new \App\Api\Repositories\User\UsersRepositories())->delete_cache($form->model()->id);
                });
            }
            $form->hidden('is_login');
            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();
        });
    }
}
